package com.txunda.zhuohired.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.txunda.zhuohired.R;
import com.txunda.zhuohired.util.wheel.NumericWheelAdapter;
import com.txunda.zhuohired.util.wheel.OnWheelChangedListener;
import com.txunda.zhuohired.util.wheel.OnWheelScrollListener;
import com.txunda.zhuohired.util.wheel.WheelView;

import java.util.Calendar;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/03/05 14:48
 * 功能描述：参加工作时间dialog
 * 联系方式：jiahuanchen@126.com
 */

public class JoinJobDialog extends Dialog implements View.OnClickListener {
    private JoinJobListener joinJobListener;
    private Context context;
    private WheelView wl_dialog_join_job_year;//年
    private WheelView wl_dialog_join_job_month;
    private int curYear;
    private int curMonth;
    private String date = "";
    private String join_status = "";//1 开始时间 2 参加工作时间
    private TextView tv_dialog_join_job;
    private TextView tv_dialog_join_job_no;

    public JoinJobDialog(Context context, String join_status, JoinJobListener joinJobListener) {
        super(context, R.style.dialog_style);
        this.context = context;
        this.joinJobListener = joinJobListener;
        this.join_status = join_status;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_join_job);
        findViewById(R.id.tv_dialog_join_job_cancel).setOnClickListener(this);
        tv_dialog_join_job_no = findViewById(R.id.tv_dialog_join_job_no);
        tv_dialog_join_job_no.setOnClickListener(this);
        findViewById(R.id.tv_dialog_join_job_sure).setOnClickListener(this);
        tv_dialog_join_job = findViewById(R.id.tv_dialog_join_job);
        wl_dialog_join_job_year = findViewById(R.id.wl_dialog_join_job_year);
        wl_dialog_join_job_month = findViewById(R.id.wl_dialog_join_job_month);
        if ("1".equals(join_status)) {
            tv_dialog_join_job_no.setVisibility(View.GONE);
            tv_dialog_join_job.setText("开始时间");
        } else if ("2".equals(join_status)) {
            tv_dialog_join_job_no.setVisibility(View.VISIBLE);
            tv_dialog_join_job.setText("参加工作时间");
        }
        initWheelView();
    }


    @Override
    public void onClick(View view) {
        this.dismiss();
        switch (view.getId()) {
            case R.id.tv_dialog_join_job_sure://确认
                joinJobListener.onJoinJob(date);
                break;
            case R.id.tv_dialog_join_job_no://无工作经验
                joinJobListener.onJoinJob("无工作经验");
                break;
        }
    }

    public interface JoinJobListener {
        void onJoinJob(String birthday);
    }

    private void initWheelView() {
        Calendar c = Calendar.getInstance();
        curYear = c.get(Calendar.YEAR);
        curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        String month = "";
        if (curMonth < 10) {
            month = "0" + curMonth;
        } else {
            month = "" + curMonth;
        }

        date = curYear + "-" + month;


        NumericWheelAdapter numericWheelAdapterStart1 = new NumericWheelAdapter(context, 2000, 2100);
        numericWheelAdapterStart1.setLabel("年");
        wl_dialog_join_job_year.setViewAdapter(numericWheelAdapterStart1);
        numericWheelAdapterStart1.setTextColor(R.color.black);
        numericWheelAdapterStart1.setTextSize(20);
        wl_dialog_join_job_year.setCyclic(true);//是否可循环滑动
        wl_dialog_join_job_year.addScrollingListener(startScrollListener);
        wl_dialog_join_job_year.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                curYear = newValue + 2000;
                //                initStartDayAdapter();
            }
        });

        NumericWheelAdapter numericWheelAdapterStart2 = new NumericWheelAdapter(context, 1, 12, "%02d");
        numericWheelAdapterStart2.setLabel("月");
        wl_dialog_join_job_month.setViewAdapter(numericWheelAdapterStart2);
        numericWheelAdapterStart2.setTextColor(R.color.black);
        numericWheelAdapterStart2.setTextSize(20);
        wl_dialog_join_job_month.setCyclic(true);
        wl_dialog_join_job_month.addScrollingListener(startScrollListener);
        wl_dialog_join_job_month.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                curMonth = newValue + 1;
                //                initStartDayAdapter();
            }
        });
        //        initStartDayAdapter();


        wl_dialog_join_job_year.setCurrentItem(curYear - 2000);
        wl_dialog_join_job_month.setCurrentItem(curMonth - 1);
        //        wl_select_time_day.setCurrentItem(curDate - 1);


    }

    //    private void initStartDayAdapter() {
    //        NumericWheelAdapter numericWheelAdapterStart3 = new NumericWheelAdapter(context, 1, getDay(curYear,
    // curMonth)
    //                , "%02d");
    //        numericWheelAdapterStart3.setLabel("日");
    //
    //        wl_select_time_day.setViewAdapter(numericWheelAdapterStart3);
    //        numericWheelAdapterStart3.setTextColor(R.color.black);
    //        //        numericWheelAdapterStart3.setBackGroundColor(R.color.colorPrimary);
    //        numericWheelAdapterStart3.setTextSize(20);
    //        wl_select_time_day.setCyclic(true);
    //        wl_select_time_day.addScrollingListener(startScrollListener);
    //    }

    OnWheelScrollListener startScrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {
        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            int n_year = wl_dialog_join_job_year.getCurrentItem() + 2000;//年
            int n_month = wl_dialog_join_job_month.getCurrentItem() + 1;//月
            //            int n_day = wl_select_time_day.getCurrentItem() + 1;//日
            String month = "";
            //            String day = "";
            if (n_month < 10) {
                month = "0" + n_month;
            } else {
                month = "" + n_month;
            }
            //            if (n_day < 10) {
            //                day = "0" + n_day;
            //            } else {
            //                day = "" + n_day;
            //            }
            date = n_year + "-" + month;
        }
    };

    /**
     * 根据年月获得 这个月总共有几天
     *
     * @param year
     * @param month
     * @return
     */
    public int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }
}
